package com.fulong.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fulong.entity.DeviceStatusInfoEntity;
import com.fulong.service.DeviceStatusInfoService;
import com.fulong.common.utils.PageUtils;
import com.fulong.common.utils.R;



/**
 * 
 *
 * @author fulong
 * @email ${email}
 * @date 2022-04-19 09:45:23
 */
@RestController
@RequestMapping("/devicestatusinfo")
public class DeviceStatusInfoController {
    @Autowired
    private DeviceStatusInfoService deviceStatusInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(){
        List<DeviceStatusInfoEntity> list = deviceStatusInfoService.list();
        return R.ok().put("list", list);
    }
    @RequestMapping("/tree")
    public R withTree(@RequestParam Map<String, Object> params){
        List<DeviceStatusInfoEntity> list = deviceStatusInfoService.withTree();
        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		DeviceStatusInfoEntity deviceStatusInfo = deviceStatusInfoService.getById(id);

        return R.ok().put("deviceStatusInfo", deviceStatusInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody DeviceStatusInfoEntity deviceStatusInfo){
         Map<String, Object> statuscode = deviceStatusInfoService.getMap(new QueryWrapper<DeviceStatusInfoEntity>().orderByDesc("statuscode"));
        if(statuscode!=null) {
            deviceStatusInfo.setStatuscode((int) statuscode.get("statuscode") + 1);
        } else {
            deviceStatusInfo.setStatuscode(0);
        }
        if(deviceStatusInfo.getEndcode()==null) {
            deviceStatusInfo.setEndcode(deviceStatusInfo.getStatuscode());
        }
        deviceStatusInfoService.save(deviceStatusInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody DeviceStatusInfoEntity deviceStatusInfo){
		deviceStatusInfoService.updateById(deviceStatusInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		deviceStatusInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/getoption")
    public R getOption(@RequestBody Integer statuscode) {
        DeviceStatusInfoEntity deviceStatusInfoEntity = deviceStatusInfoService.getOne(new QueryWrapper<DeviceStatusInfoEntity>().eq("statuscode", statuscode));
        String id = deviceStatusInfoEntity.getId();
        List<DeviceStatusInfoEntity> list = deviceStatusInfoService.list(new QueryWrapper<DeviceStatusInfoEntity>().eq("parent", id));
        return R.ok().put("option" , list);
    }
}
